using LiteDB;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LiteDb
{
    /// <summary>
    /// API响应代码枚举
    /// </summary>
    public enum ApiResponseCode
    {
        /// <summary>
        /// 成功
        /// </summary>
        Success = 0,
        
        /// <summary>
        /// 一般错误
        /// </summary>
        Error = 1,
        
        /// <summary>
        /// 数据不存在
        /// </summary>
        NotFound = 4,
        
        /// <summary>
        /// 方法不存在
        /// </summary>
        MethodNotFound = 3
    }

    public class ApiResponse
    {
        public int code { get; set; }
        public string? msg { get; set; }
        public object? data { get; set; }
    
        public static ApiResponse Success(object? data = null, string msg = "操作成功")
            => new ApiResponse { code = (int)ApiResponseCode.Success, msg = msg, data = data };
    
        public static ApiResponse Fail(string msg = "操作失败", object? data = null, ApiResponseCode code = ApiResponseCode.Error)
            => new ApiResponse { code = (int)code, msg = msg, data = data };
    
        public static ApiResponse NotFound(string msg = "方法不存在")
            => new ApiResponse { code = (int)ApiResponseCode.MethodNotFound, msg = msg, data = new { } };
            
        /// <summary>
        /// 检查响应是否成功
        /// </summary>
        public bool IsSuccess => code == (int)ApiResponseCode.Success;
        
        /// <summary>
        /// 检查是否为方法不存在错误
        /// </summary>
        public bool IsMethodNotFound => code == (int)ApiResponseCode.MethodNotFound;
        
        /// <summary>
        /// 检查是否为数据不存在错误
        /// </summary>
        public bool IsNotFound => code == (int)ApiResponseCode.NotFound;
    }
} 